<?php
namespace PsiFramework\Applications\Bibliotheque\Classes\Actions\CMS;

import("Classes.Factory.DAOFactory");
import("Classes.Factory.ObjectFactory");
import("Plugins.CMS.Classes.Actions.SaveAction");

use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Factory\ObjectFactory;
use \PsiFramework\Plugins\CMS\Classes\Actions\SaveAction;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action de sauvegarde
 *
 * @package Actions
 */
class BibliothequeSaveAction extends SaveAction
{

    public function getAuteur(IDAO $dao, Object $obj)
    {
        $obj->setNom($this->_request->getParameter('nom'));
        $obj->setPrenom($this->_request->getParameter('prenom'));

        return $obj ;
    }

    public function getGenre(IDAO $dao, Object $obj)
    {
        $obj->setNom($this->_request->getParameter('nom'));

        return $obj ;
    }

    public function getOeuvre(IDAO $dao, Object $obj)
    {
        $titre = $this->_request->getParameter('titre');
        $obj->setTitre($titre);
        
        $annee = $this->_request->getParameter('annee');
        $obj->setAnnee($annee);
        
        $notes = $this->_request->getParameter('notes');
        $obj->setNotes($notes);
        
        $pret  = $this->_request->getParameter('pret');
        $obj->setPret($pret);

        $typeId = $this->_request->getParameter('type_id');
        $type = null ;
        if ($typeId != null) {
            $typeDao = DAOFactory::getDAO('Type');
            $type = $typeDao->retrieveByPk($typeId);
        }
        $obj->setType($type);

        $dao->save($obj, false);

        $contributionDao = ContributionDAO::getInstance();
        $cs = array();

        for ($i = 0; true; $i++) {

            $auteurName = $this->_request->getParameter('auteur_'.$i);
            $delete = $this->_request->getParameter('delete_auteur_'.$i);

            if ($auteurName != null && $auteurName != "") {
                if (!$delete) {
                    $auteurId =
                        $this->_request->getParameter('auteur_'.$i.'_id');

                    $auteur = null ;
                    $auteurDao = AuteurDAO::getInstance();
                    if ($auteurId != null && $auteurId != "") {
                        $auteur = $auteurDao->retrieveByPk($auteurId);
                    }
                    if ($auteur == null) {
                        $auteur = new Auteur(null, $auteurName);
                        $auteurDao->save($auteur, false);
                    }

                    $roleDao = RoleDAO::getInstance();
                    $role = $roleDao->retrieveByPk(
                        $this->_request->getParameter('role_'.$i.'_id')
                    );

                    $contribution = new Contribution($auteur, $role, $obj);

                    $cs[$i] = $contribution ;
                    $contributionDao->save($contribution, false);
                }
            } else {
                break;
            }
        }

        $obj->setContributions($cs);

        return $obj ;
    }

    public function getRole(IDAO $dao, Object $obj)
    {
        $obj->setNom($this->_request->getParameter('nom'));

        return $obj ;
    }

    public function getType(IDAO $dao, Object $obj)
    {
        $obj->setNom($this->_request->getParameter('nom'));

        $typeId = $this->_request->getParameter('parent_type_id');
        $type = null ;
        if ($typeId !== null && $typeId !== "") {
            $typeDao = DAOFactory::getDAO('Type');
            $type = $typeDao->retrieveByPk($typeId);
        }
        $obj->setParent($type);
        $dao->save($obj, false);

        for ($i = 0; true; $i++) {

            $subtypeName = $this->_request->getParameter('subtype_'.$i);
            $delete = $this->_request->getParameter('delete_subtype_'.$i);

            if ($subtypeName !== null && $subtypeName !== "") {

                $subtypeId = $this->_request->getParameter('subtype_'.$i.'_id');

                $subtype = null ;
                if ($subtypeId !== null && $subtypeId !== "") {
                    $subtype = $dao->retrieveByPk($subtypeId);
                }

                if ($subtype == null) {
                    $subtype = new Type(null, $subtypeName, $obj);
                    $dao->save($subtype, false);
                } else if ($delete) {
                    $dao->delete($subtype);
                    $subtype = null ;
                }

                if ($subtype != null) $obj->addChild($subtype);
            } else {
                break;
            }
        }

        return $obj ;
    }

}
